package com.pinda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pinda.base.entity.PdTransportLine;
import com.pinda.base.transportline.TransportLineDto;
import com.pinda.commonutils.Page;
import com.pinda.commonutils.Result;

import com.pinda.service.IPdTransportLineService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 线路表 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd_transport_line")
public class PdTransportLineController {


    @Autowired
    IPdTransportLineService iPdTransportLineService;

    /**
     * 获取线路列表
     *
     * @param ids 线路id列表
     * @return 线路列表
     */
    @GetMapping("")
    public List<TransportLineDto> findAll(@RequestParam(name = "ids", required = false) List<String> ids,
                                          @RequestParam(name = "agencyId", required = false) String agencyId,
                                          @RequestParam(name = "agencyIds", required = false) List<String> agencyIds) {
        return iPdTransportLineService.findAll(ids, agencyId, agencyIds).stream().map(pdTransportLine -> {
            TransportLineDto dto = new TransportLineDto();
            BeanUtils.copyProperties(pdTransportLine, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    /**
     * 根据id获取线路详情
     *
     * @param id 线路id
     * @return 线路详情
     */
    @GetMapping("/{id}")
    public TransportLineDto fineById(@PathVariable(name = "id") String id) {
        PdTransportLine pdTransportLine = iPdTransportLineService.getById(id);
        TransportLineDto dto = new TransportLineDto();
        if (pdTransportLine != null) {
            BeanUtils.copyProperties(pdTransportLine, dto);
        }else {
            dto.setId(id);
        }
        return dto;
    }


    /**
     * 获取线路列表
     *
     * @return 线路列表
     */
    @PostMapping("list")
    public List<TransportLineDto> list(@RequestBody TransportLineDto transportLineDto) {
        LambdaQueryWrapper<PdTransportLine> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(StringUtils.isNotEmpty(transportLineDto.getStartAgencyId()), PdTransportLine::getStartAgencyId, transportLineDto.getStartAgencyId());
        wrapper.eq(StringUtils.isNotEmpty(transportLineDto.getEndAgencyId()), PdTransportLine::getEndAgencyId, transportLineDto.getEndAgencyId());
        wrapper.eq(StringUtils.isNotEmpty(transportLineDto.getAgencyId()), PdTransportLine::getAgencyId, transportLineDto.getAgencyId());
        wrapper.eq(null != (transportLineDto.getStatus()), PdTransportLine::getStatus, transportLineDto.getStatus());

        return iPdTransportLineService.list(wrapper).stream().map(pdTransportLine -> {
            TransportLineDto dto = new TransportLineDto();
            BeanUtils.copyProperties(pdTransportLine, dto);
            return dto;
        }).collect(Collectors.toList());
    }

    /**
     * 分页查询线路
     * @param page
     * @return
     */
    @PostMapping("/queryPageTransportLine")
    public Result queryPageTransportLine(@RequestBody Page<PdTransportLine> page){
        return this.iPdTransportLineService.queryPageTransportLine(page);
    }


    /**
     * 根据id修改线路类型
     * @param pdTransportLine
     * @return
     */
    @PostMapping("/updataTransportLine")
    public boolean  updataTransportLine(@RequestBody PdTransportLine pdTransportLine){
        return this.iPdTransportLineService.updateById(pdTransportLine);
    }





    /**
     * 添加线路类型
     * @param pdTransportLine
     * @return
     */
    @PostMapping("/addTransportLine")
    public boolean  addTransportLine(@RequestBody PdTransportLine pdTransportLine){
        return this.iPdTransportLineService.save(pdTransportLine);
    }



    @GetMapping("/deleteTransportLine")
    public Boolean deleteTransportLine(Integer id) {

        return this.iPdTransportLineService.removeById(id);
    }
}
